package com.bml.architect.spring.dlx;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;

public class DlxListener implements ChannelAwareMessageListener {
    @Override
    public void onMessage(Message message, Channel channel) throws Exception {

        //消息ID
        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        try{
            String msg = new String(message.getBody());
            System.out.println("接受到的消息："+msg);
            System.out.println("业务处理中...");
            Thread.sleep(500);
            //正常签收
            //第二个参数false的意思就是说只签收本次消息，true表示签收之前的所有未签收的
            channel.basicAck(deliveryTag,false);
            System.out.println("处理完成");

        }catch (Exception ce){
            //第三个参数：requeue：重回队列。如果设置为true，则消息重新回到queue，broker会重新发送该消息给消费端
            channel.basicNack(deliveryTag,false,true);
        }

    }

}
